package org.come.Jpanel;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import org.come.bean.AllActive;
import org.come.model.ActiveAward;
import org.come.model.ActiveBase;
import org.come.until.*;

import com.tool.ModerateTask.TaskRoleData;
import com.tool.btn.ActivityBtn;
import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

/**
 * 活动主面板 编号:40
 *
 * @author HGC<br>
 * @time 2020年3月2日 上午10:05:41<br>
 * @class 类名:ActivityJpanel<br>
 */
public class ActivityJpanel extends JPanel {

    /** 活动列表，活动详情 */
    private JScrollPane paneList, paneDetails;
    /** 活跃度 */
    private JLabel[] labVitality;

    private ActivityBtn[] btnVitality;

    private ActivityBtn tjBtn,zlBtn;

    private RichLabel richDetails;

    private JList<ActivityModelJpanel> JlistActivityModelPanel;
    private JLabel labNowVitality;
    private Map<Integer, ActivityModelJpanel> mapActivityModelPanel;

    private ActivityWeekJpanel activityWeekJpanel;
    /** 总的活跃度 */
    private int nowVitality = 0;

    public ActivityJpanel() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            setPreferredSize(new Dimension(855, 479));
            setOpaque(false);
            setLayout(null);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/button/1.png", 1, 40);
            offBtn.setBounds(855-25, 0, 25, 25);
            this.add(offBtn);

            tjBtn = new ActivityBtn("skin/8100226/newbutton/水墨今日推荐.png", 1, 9990,0,this);
            tjBtn.setBounds(0 + 48, 0 + 23, 100, 38);
            this.add(tjBtn);

            zlBtn = new ActivityBtn("skin/8100226/newbutton/水墨活动周历.png", 1, 9991,0,this);
            zlBtn.setBounds(105 + 40, 0+ 23, 100, 38);
            this.add(zlBtn);

            activityWeekJpanel = new ActivityWeekJpanel();
            activityWeekJpanel.setBounds(0, 479-416, 855, 416); // 设置 WestboundLevelSubJpanel 的位置和大小
            activityWeekJpanel.setVisible(false);
            add(activityWeekJpanel);



            getPaneList();
            getPaneDetails();
            getLabNowVitality();

            showView();
        }else {

            setPreferredSize(new Dimension(855, 479));
            setOpaque(false);
            setLayout(null);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/s74.png", 1, 40);
            offBtn.setBounds(855-25, 0, 25, 25);
            this.add(offBtn);


            tjBtn = new ActivityBtn("inkImg/hongmu/活动选项1.png", 1, 9990,0,this);
            tjBtn.setBounds(0 + 40, 0 + 23, 100, 38);
            this.add(tjBtn);

            zlBtn = new ActivityBtn("inkImg/hongmu/活动选项2.png", 1, 9991,0,this);
            zlBtn.setBounds(105 + 40, 0+ 23, 100, 38);
            this.add(zlBtn);

            activityWeekJpanel = new ActivityWeekJpanel();
            activityWeekJpanel.setBounds(0, 479-416, 855, 416); // 设置 WestboundLevelSubJpanel 的位置和大小
            activityWeekJpanel.setVisible(false);
            add(activityWeekJpanel);



            getPaneList();
            getPaneDetails();
            getLabNowVitality();

            showView();
        }
    }

    /** 列表鼠标监听 */
    class PaneListMouse extends MouseAdapter {
        private ActivityModelJpanel activityModelJpanel;

        public PaneListMouse(ActivityModelJpanel activityModelJpanel) {
            this.activityModelJpanel = activityModelJpanel;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            ActiveBase activeBase = activityModelJpanel.getActiveBase();
            if (activeBase == null)
                return;
            richDetails.setText(activeBase.getText());
            Dimension d = richDetails.computeSize(166);
            richDetails.setSize(d);
            richDetails.setPreferredSize(d);
        }

    }

    /** 展示面板 */
    public void showView() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            AllActive allActive = UserMessUntil.getAllActive();
            if (allActive == null) {
                return;
            }
            nowVitality = 0;
            mapActivityModelPanel = new HashMap<Integer, ActivityModelJpanel>();
            ActiveBase[] activeBases = allActive.getBases();

            JlistActivityModelPanel.setLayout(new GridLayout(0, 3));
            for (int i = 0; i < activeBases.length; i++) {
                ActiveBase activeBase = activeBases[i];
                int sumReceive = TaskRoleData.SumReceive(activeBase.getSid(), 2);
                ActivityModelJpanel activityModelJpanel = new ActivityModelJpanel();
                activityModelJpanel.showActiveBase(activeBase, sumReceive);

                if (activeBase.getNum() < sumReceive) {
                    sumReceive = activeBase.getNum();
                }

                nowVitality += (sumReceive * activeBase.getValue());
                activityModelJpanel.addMouseListener(new PaneListMouse(activityModelJpanel));
                JlistActivityModelPanel.add(activityModelJpanel);
                mapActivityModelPanel.put(activeBase.getSid(), activityModelJpanel);
            }
            JlistActivityModelPanel.setPreferredSize(new Dimension(490, 61/3 * activeBases.length));
            labNowVitality.setText(String.valueOf(nowVitality));

            ActiveAward[] awardsz = allActive.getAwards();
            ActiveAward[] awards = new ActiveAward[6];
            if(awardsz!=null && awardsz.length>0) {
                for(int i=0;i<awardsz.length;i++) {
                    if(awardsz[i].getActive()>0) {
                        awards[i] = awardsz[i];
                    }
                }
            }
            labVitality = new JLabel[awards.length];
            btnVitality = new ActivityBtn[awards.length];
            int num = 340 / awards.length;
            int sumReceive = TaskRoleData.SumReceive(2, 3);
            for (int i = 0; i < awards.length; i++) {
                labVitality[i] = new JLabel(String.valueOf(awards[i].getActive()));
                labVitality[i].setBounds(90 + num * (i + 1), 443, 45, 20);
                labVitality[i].setForeground(Color.white);
                labVitality[i].setHorizontalAlignment(SwingConstants.CENTER);
                labVitality[i].setFont(UIUtils.TEXT_MSG);
                labVitality[i].setOpaque(false);
                this.add(labVitality[i]);
                btnVitality[i] = new ActivityBtn("inkImg/button/B308.png", 1, 1, i, this);
                btnVitality[i].setName(awards[i].getValue());
                if (nowVitality >= awards[i].getActive()) {
                    int c = (sumReceive >> i) & 0x01;
                    if (c == 1) {
                        btnVitality[i].setBtn(-1);
                        btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B310.png", -1, -1));
                    }
                } else {
                    btnVitality[i].setBtn(-1);
                    btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B309.png", -1, -1));
                }
                btnVitality[i].setBounds(90 + num * (i + 1), 412, 45, 36);
                this.add(btnVitality[i]);
            }
        }else {
            AllActive allActive = UserMessUntil.getAllActive();
            if (allActive == null) {
                return;
            }
            nowVitality = 0;
            mapActivityModelPanel = new HashMap<Integer, ActivityModelJpanel>();
            ActiveBase[] activeBases = allActive.getBases();

            JlistActivityModelPanel.setLayout(new GridLayout(0, 3));
            for (int i = 0; i < activeBases.length; i++) {
                ActiveBase activeBase = activeBases[i];
                int sumReceive = TaskRoleData.SumReceive(activeBase.getSid(), 2);
                ActivityModelJpanel activityModelJpanel = new ActivityModelJpanel();
                activityModelJpanel.showActiveBase(activeBase, sumReceive);

                if (activeBase.getNum() < sumReceive) {
                    sumReceive = activeBase.getNum();
                }

                nowVitality += (sumReceive * activeBase.getValue());
                activityModelJpanel.addMouseListener(new PaneListMouse(activityModelJpanel));
                JlistActivityModelPanel.add(activityModelJpanel);
                mapActivityModelPanel.put(activeBase.getSid(), activityModelJpanel);
            }
            JlistActivityModelPanel.setPreferredSize(new Dimension(490, 61/3 * activeBases.length));
            labNowVitality.setText(String.valueOf(nowVitality));

            ActiveAward[] awardsz = allActive.getAwards();
            ActiveAward[] awards = new ActiveAward[6];
            if(awardsz!=null && awardsz.length>0) {
                for(int i=0;i<awardsz.length;i++) {
                    if(awardsz[i].getActive()>0) {
                        awards[i] = awardsz[i];
                    }
                }
            }
            labVitality = new JLabel[awards.length];
            btnVitality = new ActivityBtn[awards.length];
            int num = 340 / awards.length;
            int sumReceive = TaskRoleData.SumReceive(2, 3);
            for (int i = 0; i < awards.length; i++) {
                labVitality[i] = new JLabel(String.valueOf(awards[i].getActive()));
                labVitality[i].setBounds(90 + num * (i + 1), 443, 45, 20);
                labVitality[i].setForeground(Color.white);
                labVitality[i].setHorizontalAlignment(SwingConstants.CENTER);
                labVitality[i].setFont(UIUtils.TEXT_MSG);
                labVitality[i].setOpaque(false);
                this.add(labVitality[i]);
                btnVitality[i] = new ActivityBtn("inkImg/button/B308.png", 1, 1, i, this);
                btnVitality[i].setName(awards[i].getValue());
                if (nowVitality >= awards[i].getActive()) {
                    int c = (sumReceive >> i) & 0x01;
                    if (c == 1) {
                        btnVitality[i].setBtn(-1);
                        btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B310.png", -1, -1));
                    }
                } else {
                    btnVitality[i].setBtn(-1);
                    btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B309.png", -1, -1));
                }
                btnVitality[i].setBounds(90 + num * (i + 1), 412, 45, 36);
                this.add(btnVitality[i]);
            }
        }

    }

    /** 全体刷新面板 */
    public void refreshView() {
        AllActive allActive = UserMessUntil.getAllActive();
        ActiveBase[] activeBases = allActive.getBases();
        nowVitality = 0;
        for (int i = 0; i < activeBases.length; i++) {
            ActiveBase activeBase = activeBases[i];
            int sumReceive = TaskRoleData.SumReceive(activeBase.getSid(), 2);
            ActivityModelJpanel activityModelJpanel = mapActivityModelPanel.get(activeBase.getSid());
            activityModelJpanel.showActiveBase(activeBase, sumReceive);
            if (activeBase.getNum() < sumReceive) {
                sumReceive = activeBase.getNum();
            }
            nowVitality += (sumReceive * activeBase.getValue());
        }
        labNowVitality.setText(String.valueOf(nowVitality));
        ActiveAward[] awardsz = allActive.getAwards();
        ActiveAward[] awards = new ActiveAward[6];//修改活跃礼包数量
        if(awardsz!=null && awardsz.length>0) {
            for(int i=0;i<awardsz.length;i++) {
                if(awardsz[i].getActive()>0) {
                    awards[i] = awardsz[i];
                }
            }
        }
        int sumReceive = TaskRoleData.SumReceive(2, 3);
        for (int i = 0; i < awards.length; i++) {
            ActiveAward activeAward = awards[i];
            if (activeAward.getActive() > nowVitality) {
                btnVitality[i].setBtn(-1);
                btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B309.png", -1, -1));
            } else {
                int c = (sumReceive >> i) & 0x01;
                if (c == 1) {
                    btnVitality[i].setBtn(-1);
                    btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B310.png", -1, -1));
                } else {
                    btnVitality[i].setBtn(1);
                    try {
                        btnVitality[i].setIcons(CutButtonImage.cuts("inkImg/button/B308.png"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /** 局部活动任务刷新 */
    public void partRefreshView(int sid) {
        if (sid == 2) {
            int sum = TaskRoleData.SumReceive(2, 3);
            for (int i = 0; i < labVitality.length; i++) {
                int parseInt = Integer.parseInt(labVitality[i].getText());
                if (parseInt > nowVitality) {
                    btnVitality[i].setBtn(-1);
                    btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B309.png", -1, -1));
                    break;
                } else {
                    int c = (sum >> i) & 0x01;
                    if (c == 1) {
                        btnVitality[i].setBtn(-1);
                        btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B310.png", -1, -1));
                    } else {
                        btnVitality[i].setBtn(1);
                        try {
                            btnVitality[i].setIcons(CutButtonImage.cuts("inkImg/button/B308.png"));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } else {
            ActivityModelJpanel activityModelJpanel = mapActivityModelPanel.get(sid);
            if (activityModelJpanel == null) {
                return;
            }
            ActiveBase activeBase = activityModelJpanel.getActiveBase();
            int sumReceive = TaskRoleData.SumReceive(sid, 2);
            activityModelJpanel.showActiveBase(activeBase, sumReceive);
            if (activeBase.getNum() < sumReceive) {
                sumReceive = activeBase.getNum();
            } else {
                nowVitality += activeBase.getValue();
            }
            int sum = TaskRoleData.SumReceive(2, 3);
            for (int i = 0; i < labVitality.length; i++) {
                int parseInt = Integer.parseInt(labVitality[i].getText());
                if (parseInt > nowVitality) {
                    btnVitality[i].setBtn(-1);
                    btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B309.png", -1, -1));
                    break;
                } else {
                    int c = (sum >> i) & 0x01;
                    if (c == 1) {
                        btnVitality[i].setBtn(-1);
                        btnVitality[i].setIcon(CutButtonImage.getImage("inkImg/button/B310.png", -1, -1));
                    } else {
                        btnVitality[i].setBtn(1);
                        try {
                            btnVitality[i].setIcons(CutButtonImage.cuts("inkImg/button/B308.png"));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            labNowVitality.setText(String.valueOf(nowVitality));
        }

    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (icon == null) {
                icon = new ImageIcon("inkImg/background/S176.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 855, 479, this);
            g.setColor(Color.white);
        }else {
            if (icon == null) {
                icon = new ImageIcon("img/xy2uiimg/S176.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 855, 479, this);
            g.setColor(Color.white);
        }
    }

    public JScrollPane getPaneList() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (paneList == null) {
                paneList = new JScrollPane(getJlistActivityModelPanel());
                paneList.setVerticalScrollBarPolicy(22);
                paneList.getVerticalScrollBar().setUI(new ScrollUI());
                paneList.getVerticalScrollBar().setUnitIncrement(20);
                paneList.getViewport().setOpaque(false);
                paneList.setOpaque(false);
                paneList.setBounds(49, 81, 386 + 205, 310);
                paneList.setBorder(BorderFactory.createEmptyBorder());
                paneList.setHorizontalScrollBarPolicy(31);
                this.add(paneList);
            }
        }else {
            if (paneList == null) {
                paneList = new JScrollPane(getJlistActivityModelPanel());
                paneList.setVerticalScrollBarPolicy(22);
                paneList.getVerticalScrollBar().setUI(new ScrollUI());
                paneList.getVerticalScrollBar().setUnitIncrement(20);
                paneList.getViewport().setOpaque(false);
                paneList.setOpaque(false);
                paneList.setBounds(49, 81, 386 + 205, 310);
                paneList.setBorder(BorderFactory.createEmptyBorder());
                paneList.setHorizontalScrollBarPolicy(31);
                this.add(paneList);
            }
        }
        return paneList;
    }

    public void setPaneList(JScrollPane paneList) {
        this.paneList = paneList;
    }

    public JScrollPane getPaneDetails() {
        if(MyIsif.getStyle().equals("水墨UI")) {
            if (paneDetails == null) {
                paneDetails = new JScrollPane(getRichDetails());
                paneDetails.setVerticalScrollBarPolicy(22);
                paneDetails.getVerticalScrollBar().setUI(new ScrollUI());
                paneDetails.getVerticalScrollBar().setUnitIncrement(20);
                paneDetails.getViewport().setOpaque(false);
                paneDetails.setOpaque(false);
                paneDetails.setBounds(442+ 205, 81, 181, 310);
                paneDetails.setBorder(BorderFactory.createEmptyBorder());
                paneDetails.setHorizontalScrollBarPolicy(31);
                this.add(paneDetails);
            }
        }else {
            if (paneDetails == null) {
                paneDetails = new JScrollPane(getRichDetails());
                paneDetails.setVerticalScrollBarPolicy(22);
                paneDetails.getVerticalScrollBar().setUI(new ScrollUI());
                paneDetails.getVerticalScrollBar().setUnitIncrement(20);
                paneDetails.getViewport().setOpaque(false);
                paneDetails.setOpaque(false);
                paneDetails.setBounds(442+ 205, 81, 181, 310);
                paneDetails.setBorder(BorderFactory.createEmptyBorder());
                paneDetails.setHorizontalScrollBarPolicy(31);
                this.add(paneDetails);
            }
        }
        return paneDetails;
    }

    public void setPaneDetails(JScrollPane paneDetails) {
        this.paneDetails = paneDetails;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public JLabel[] getLabVitality() {
        return labVitality;
    }

    public void setLabVitality(JLabel[] labVitality) {
        this.labVitality = labVitality;
    }

    public ActivityBtn[] getBtnVitality() {
        return btnVitality;
    }

    public void setBtnVitality(ActivityBtn[] btnVitality) {
        this.btnVitality = btnVitality;
    }

    public RichLabel getRichDetails() {
        if (richDetails == null) {
            richDetails = new RichLabel("", UIUtils.TEXT_FONT1);
        }
        return richDetails;
    }

    public void setRichDetails(RichLabel richDetails) {
        this.richDetails = richDetails;
    }

    public JList<ActivityModelJpanel> getJlistActivityModelPanel() {
        if (JlistActivityModelPanel == null) {
            JlistActivityModelPanel = new JList<ActivityModelJpanel>();
            JlistActivityModelPanel.setSelectionBackground(new Color(122, 117, 112));
            JlistActivityModelPanel.setSelectionForeground(Color.white);
            JlistActivityModelPanel.setForeground(Color.white);
            JlistActivityModelPanel.setFont(UIUtils.TEXT_HY16);
            JlistActivityModelPanel.removeAll();
            JlistActivityModelPanel.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
            DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
                @Override
                public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
                                                              boolean isSelected, boolean cellHasFocus) {
                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    return this;
                }
            };
            JlistActivityModelPanel.setCellRenderer(cellRenderer);
            JlistActivityModelPanel.setOpaque(false);
        }
        return JlistActivityModelPanel;

    }

    public void setJlistActivityModelPanel(JList<ActivityModelJpanel> jlistActivityModelPanel) {
        JlistActivityModelPanel = jlistActivityModelPanel;
    }

    public JLabel getLabNowVitality() {
        if (labNowVitality == null) {
            labNowVitality = new JLabel();
            labNowVitality.setHorizontalAlignment(SwingConstants.CENTER);
            labNowVitality.setForeground(Color.white);
            labNowVitality.setFont(UIUtils.TEXT_FONT1);
            labNowVitality.setBounds(700, 440, 68, 16);
            this.add(labNowVitality);
        }
        return labNowVitality;
    }

    public void setLabNowVitality(JLabel labNowVitality) {
        this.labNowVitality = labNowVitality;
    }
    public Map<Integer, ActivityModelJpanel> getMapActivityModelPanel() {
        return mapActivityModelPanel;
    }

    public void setMapActivityModelPanel(Map<Integer, ActivityModelJpanel> mapActivityModelPanel) {
        this.mapActivityModelPanel = mapActivityModelPanel;
    }

    public ActivityWeekJpanel getActivityWeekJpanel() {
        return activityWeekJpanel;
    }

    public void setActivityWeekJpanel(ActivityWeekJpanel activityWeekJpanel) {
        this.activityWeekJpanel = activityWeekJpanel;
    }
}
